<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>

<script src="<c:url value='/js/courseEditCourseTarget.js'/>" type="text/javascript" charset="utf-8"></script>
<!--课程目标 弹出框-->

<div class="row">
    <div class="col-md-12 col-lg-12 editContent">
        <div class="row">
            <div class="newCourse-label">课程目标</div>
            <div class="col-md-2 col-lg-2"></div>
            <div class="col-md-8 col-lg-8">
                <input id="target_num" hidden="hidden" value="1">
                <table class="table">
                    <thead id="courseTargetList" class="couserTargetList">
                    <tr>
                        <td class="col-md-2 col-lg-2"><label for="courseTargetNum"
                                                             style="font-size: 16px">编号</label></td>
                        <td class="col-md-5 col-lg-5"><label for="courseTargetDetail"
                                                             style="font-size: 16px">课程目标内容</label></td>
                        <td class="col-md-2col-lg-2"><label for="graduationRequirement"
                                                             style="font-size: 16px">毕业要求</label></td>
                        <td class="col-md-3 col-lg-2"><label for="weight" style="font-size: 16px">比重</label></td>
                    </tr>
                    <tr class="courseTarget">
                        <td><label style="font-size: 16px">课程目标1</label></td>
                        <td><input type="text" name="courseTargetDetail" class="form-control"></td>
                        <td><input type="text" name="graduationRequirement" class="form-control"></td>
                        <td><input type="text" name="targetWeigh" class="form-control"></td>
                        <td style="font-size: 22px">%</td>
                    </tr>
                    </thead>

                </table>
                <button type="button" class="btn btn-primary" onclick="addCourseTarget()">添加课程目标</button>
            </div>

        </div>

        <div class="row">
            <div class="newCourse-label">评价方式</div>
            <div class="col-md-2 col-lg-2"></div>
            <div class="col-md-8 col-lg-8">
                <input id="check_type" hidden="hidden" value="1">
                <table class="table">
                    <thead id="checkTypeList">
                    <tr class="checkType">
                        <td class="col-md-3 col-lg-3" style="font-size: 16px"><label>评价方式1</label></td>
                        <td class="col-md-4 col-lg-4">
                            <input type="text" class="form-control" name="checkTypeDetail">
                        </td>
                        <td class="col-md-2 col-lg-2" style="font-size: 16px"><label>比重</label></td>
                        <td class="col-md-2 col-lg-2"><input type="text" class="form-control" name="typeWeight"></td>
                        <td class="col-md-1 col-lg-1" style="font-size: 22px">%</td>
                    </tr>
                    </thead>

                </table>
                <button type="button" class="btn btn-primary" onclick="addCheckType()">添加评价方式</button>
            </div>

        </div>
        <div style="text-align:center; margin-top: 40px">
            <button class="btn btn-primary" onclick="submitTarget()">确定</button>
        </div>
    </div>
</div>

<script>
    var data =${data};
    var courseId = data.data.courseId;

    $(function () {
        //获取当前已有课程目标
        $.ajax({
            type: "GET",
            url: CONTEXT_PATH + "courseTargets/" + courseId,
            success: function (data) {
                // console.log("data:" + JSON.stringify(data));
                if (data.code != 0) {
                    window.alert(data.msg);
                    return;
                }
                var courseTargets = data.data;
                // console.log(courseTargets);
                if (courseTargets.length != 0) {
                    showCourseTarget(courseTargets);
                    //获取当前已有评价方式
                    $.ajax({
                        type: "GET",
                        url: CONTEXT_PATH + "evaluation/" + courseId,
                        success: function (data) {
                            if(data.code!=0){
                                window.alert(data.msg)
                            }
                            var checkTypes = data.data;
                            // console.log(checkTypes);

                            if (checkTypes.length != 0) {
                                showCheckTypes(checkTypes);
                            } else {
                                /////
                                alert("当前没有相关数据")
                            }
                        }
                    });

                } else {
                    /////
                }
            },
            error: function () {

            }
        });

    })

    function submitTarget() {
        //提交课程目标和评价方式
        //课程目标
        var targetWeightSum = 0;
        var typeWeightSum = 0;
        var elements = document.getElementsByClassName('courseTarget');
        var courseTargets = new Array();

        for (var i = 0; i < elements.length; i++) {

            var td = elements[i].getElementsByTagName("td");
            var targetDetail = td[1].getElementsByTagName("input")[0].value;
            var graRequirement = td[2].getElementsByTagName("input")[0].value;
            var targetWeight = td[3].getElementsByTagName("input")[0].value / 100;
            targetWeightSum += targetWeight;
            var courseTarget = {
                description: targetDetail,
                graduationRequirementNumber: graRequirement,
                weight: targetWeight
            };
            courseTargets[i] = courseTarget;
        }
        //评价方式
        var elements = document.getElementsByClassName("checkType");
        var checkTypes = new Array();

        for (var i = 0; i < elements.length; i++) {

            var td = elements[i].getElementsByTagName("td");
            var checkTypeDetail = td[1].getElementsByTagName("input")[0].value;
            var typeWeight = td[3].getElementsByTagName("input")[0].value / 100;
            typeWeightSum += typeWeight;
            var checkType = {
                evaluationType: checkTypeDetail,
                weight: typeWeight,
            };
            checkTypes[i] = checkType;
        }

        var json = {
            courseTargets: courseTargets,
            evaluationOfCourseTargets: checkTypes,
            courseId: courseId,
        };
        // console.log(json)

        if (targetWeightSum == 1 && typeWeightSum == 1) {
            //ajax 提交
            // $.post(CONTEXT_PATH + "courseTargets/ajaxSubmit", json
            //     , function (data) {
            //
            //         // location.reload();//成功就刷新
            //
            //     });

            $.ajax({
                type: "POST",
                url: CONTEXT_PATH + "courseTargets/ajaxSubmit",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(json),
                dataType: "json",
                success: function (message) {

                    if (message.code != 0) {
                        window.alert(message.msg);
                    } else {
                        location.reload();//成功就刷新
                    }
                },
                error: function (message) {

                }
            });
        } else {
            alert("各项总比重为100%")
        }

    }
</script>
